/*
 * Copyright 2011 Stefan Ferstl
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.debmavenplugin.archiver;

import java.io.File;
import java.util.Date;

import org.apache.commons.compress.archivers.ArchiveEntry;

/**
 * A simple implementation of an {@link ArchiveEntry}.
 */
public class SimpleFileArchiveEntry implements IFileArchiveEntry, Comparable<IFileArchiveEntry> {

  private final File file;
  private final String name;


  /**
   * Constructor.
   *
   * @param file File (must not be <code>null</code>).
   * @param name This entry's name. If <code>null</code> is provided, the name
   * will be the result of <code>file.getName()</code>
   */
  public SimpleFileArchiveEntry(File file, String name) {
    if (file == null) {
      throw new IllegalArgumentException("File must not be null");
    }
    this.file = file;
    this.name = (name != null) ? name : file.getName();

  }

  /** {@inheritDoc} */
  @Override
  public File getFile() {
    return this.file;
  }


  /** {@inheritDoc} */
  @Override
  public String getName() {
    return this.name;
  }

  /** {@inheritDoc} */
  @Override
  public long getSize() {
    long size = 0;
    if (!isDirectory()) {
      size = this.file.length();
    }
    return size;
  }

  /** {@inheritDoc} */
  @Override
  public boolean isDirectory() {
    return this.file.isDirectory();
  }

  /** {@inheritDoc} */
  @Override
  public int compareTo(IFileArchiveEntry o) {
    return this.file.compareTo(o.getFile());
  }

  /** {@inheritDoc} */
  @Override
  public Date getLastModifiedDate() {
    return new Date(this.file.lastModified());
  }

}
